package cn.itsource.controller;

import cn.itsource.constants.RConst;
import cn.itsource.result.JSONResult;
import cn.itsource.service.TXCosTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/common")
public class TXCosController {
    @Autowired
    private TXCosTemplate txCosTemplate;
    //上传文件到COS云服务器
    @PostMapping("/cos/upload")
    public JSONResult uploadCos(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(RConst.Str.DOT);
        String suffix = originalFilename.substring(i, originalFilename.length());
        String imgName = UUID.randomUUID().toString();
        return JSONResult.success(txCosTemplate.upload(file.getInputStream(),imgName+suffix));
    }
}
